﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Keepo.Models;
using System.Web.Mvc;

namespace AlbumDesigner.Helpers
{
    public class PageNotesHelper
    {
        public static string GetEditorTypeString(PageNote note, int? UserId)
        {
            var type = "";
            if (UserId != null)
            {
                //if the sender is the Album editor
                if (UserId == note.AlbumPage.AlbumSection.Album.EditorId)
                {
                    type = "עורך האלבום";
                }
                //if the sender is the AlbumSection editor
                if (UserId == note.AlbumPage.AlbumSection.EditorId)
                {
                    type = "עורך " + note.AlbumPage.AlbumSection.Title;
                }
            }
            else
            {
                type = "צוות דפוס";
            }

            return type;
        }

        public static void SendMailForNote(int pageNoteId, UrlHelper Url)
        {
            using (var db = new AlbumContext())
            {
                var note = db.PageNotes.Find(pageNoteId);
                var senderType = PageNotesHelper.GetEditorTypeString(note, note.SenderId);

                int pageIdxInAlbum = AlbumPagesHelper.SectionFirstPageNum(note.AlbumPage.AlbumSectionId);
                var pagesBefore = note.AlbumPage.AlbumSection.AlbumPages.Where(m => m.SectionPageIdx < note.AlbumPage.SectionPageIdx).ToList();

                //calculate the Page index in this album
                foreach (var page in pagesBefore)
                {
                    pageIdxInAlbum += page.IsSingle ? 1 : 2;
                }

                //send mail to notify about the note
                var albumName = note.AlbumPage.AlbumSection.Album.Title;
                var albumId = note.AlbumPage.AlbumSection.AlbumId;
                var mailTitle = "הודעה חדשה - " + albumName + " - " + note.AlbumPage.AlbumSection.Title;
                var mailText = "קיבלת הודעה חדשה מ" + senderType + " במסגרת עמוד " + ((pageIdxInAlbum > 0) ? pageIdxInAlbum.ToString() : "כריכה") + " באלבום \"" + albumName + "\".<br /><br /><strong>טקסט ההודעה</strong>: " + note.Text;

                var receiverEmail = "support@elinirschool.co.il";
                var albumLoginUrl = "";

                //if there is reciever for the note
                if (note.Receiver != null)
                {
                    receiverEmail = note.Receiver.Email;
                    albumLoginUrl = Url.Action("EditorLogin", "Users", new { id = note.AlbumPage.AlbumSection.Album.OrderNum }, "http");
                }
                //if there is no receiver - the reciever is admin mail
                else
                {
                    mailText += "<br /><br /><a href='" + Url.Action("Details", "Albums", new { id = albumId }, "http") + "' style='font-weight:bold;'>עמוד הפרויקט - " + albumName + "</a>";
                }

                AlbumDesigner.Helpers.MailHelper.SendMail(receiverEmail, mailTitle, mailText, albumLoginUrl);
            }
        }
    }
}